package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC219 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            Set<Integer> set = new HashSet<>();
            int left = 0, right = 0, cur = 0;

            while (right < nums.length) {
                if (right - left > k) {
                    // window too large
                    set.remove(nums[left++]);
                }

                if (set.contains(nums[right])) return true;
                set.add(nums[right++]);
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3,1,2,3]");
        System.out.println(solution.containsNearbyDuplicate(arr, 2));
    }

    public static void main(String[] args) throws Exception {
        LC219 an = new LC219();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
